package china.accountinghelper.activity;

import com.xdroid.base.AsyncTaskBase;

import china.wc.tallyhelper.R;
import china.wc.tallyhelper.adapter.AdapterListViewPersonList;
import china.wc.tallyhelper.business.BusinessPerson;
import china.wc.tallyhelper.controls.SlideMenu;
import china.wc.tallyhelper.controls.SlideMenuItem;
import china.wc.tallyhelper.model.ModelPerson;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class ActivityPerson extends ActivityFrame implements OnClickListener
{
    static final String               EDITING_MODEL_OBJECT           = "ModelPerson";
    static final int                  REQUEST_CODE_ADD_PERSON       = 1;
    static final int                  REQUEST_CODE_EDIT_PERSON      = 2;
    static final int                  RESULT_ERR                    = RESULT_FIRST_USER + 1;
    static final int                  DIALOG_CONFIRM_PERSON_DELETE  = 1;
    static final int                  DIALOG_CONFIRM_PERSON_DISABLE = 2;

    private ListView                  mFramebodyPerson_ListView_PersonList;
    private AdapterListViewPersonList mAdapterListViewPersonList;
    private BusinessPerson            mBusinessPerson;
    // 当前正在操作的实体类对象
    private ModelPerson               mSelectedModelPerson;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // 载入主页面Layout文件
        loadFramebody(R.layout.framebody_person);
        // 生成主界面的SlideMenu菜单
        creatSlideMenu(R.array.slidemenu_person);
        // 设置标题
        setTopTitle(R.string.title_person);

        executeInitTask(null);
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (mAdapterListViewPersonList!=null) {
            mAdapterListViewPersonList.notifyDataSetChanged();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        mAdapterListViewPersonList.notifyDataSetChanged();

        if (requestCode == REQUEST_CODE_ADD_PERSON) {
            if (resultCode == RESULT_OK) {
                showToastShort(R.string.message_person_add_ok);
            } else if (resultCode == RESULT_ERR) {
                showToastShort(R.string.message_person_add_err);
            }

        } else if (requestCode == REQUEST_CODE_EDIT_PERSON) {
            if (resultCode == RESULT_OK) {
                showToastShort(R.string.message_person_update_ok);
            } else if (resultCode == RESULT_ERR) {
                showToastShort(R.string.message_person_update_err);
            }
        }

    }

    /**
     * 生成人员管理页面的上下文菜单，根据人员状态，动态改变菜单项。
     * 
     */
    @Override
    public void onCreateContextMenu(ContextMenu pMenu, View pView, ContextMenuInfo pMenuInfo)
    {
        super.onCreateContextMenu(pMenu, pView, pMenuInfo);
        getMenuInflater().inflate(R.menu.contextmenu_personlist, pMenu);

        AdapterView.AdapterContextMenuInfo _MenuInfo = (AdapterView.AdapterContextMenuInfo) pMenuInfo;
        mSelectedModelPerson = mAdapterListViewPersonList.getItem(_MenuInfo.position);

        pMenu.setHeaderIcon(R.drawable.icon_rqwl_hrqc)
                .setHeaderTitle(mSelectedModelPerson.getName());

        if (mSelectedModelPerson.getState() == ModelPerson.STATE_ENABLED) {
            pMenu.findItem(R.id.ContextMenu_PersonList_EditPerson).setVisible(true).setEnabled(true);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateEnable).setVisible(false).setEnabled(false);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateDisable).setVisible(true).setEnabled(true);
            pMenu.findItem(R.id.ContextMenu_PersonList_DeletePerson).setVisible(true).setEnabled(true);
        } else if (mSelectedModelPerson.getState() == ModelPerson.STATE_DISABLED) {
            pMenu.findItem(R.id.ContextMenu_PersonList_EditPerson).setVisible(true).setEnabled(true);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateEnable).setVisible(true).setEnabled(true);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateDisable).setVisible(false).setEnabled(false);
            pMenu.findItem(R.id.ContextMenu_PersonList_DeletePerson).setVisible(true).setEnabled(true);
        } else if (mSelectedModelPerson.getState() == ModelPerson.STATE_INDISPENSABLE) {
            pMenu.findItem(R.id.ContextMenu_PersonList_EditPerson).setVisible(false).setEnabled(false);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateEnable).setVisible(false).setEnabled(false);
            pMenu.findItem(R.id.ContextMenu_PersonList_SetPersonStateDisable).setVisible(false).setEnabled(false);
            pMenu.findItem(R.id.ContextMenu_PersonList_DeletePerson).setVisible(false).setEnabled(false);
        }
    }

    /**
	 * 
	 */
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        // 修改人员
        if (item.getItemId() == R.id.ContextMenu_PersonList_EditPerson) {
            Bundle _Bundle = new Bundle();
            _Bundle.putSerializable(EDITING_MODEL_OBJECT, mSelectedModelPerson);
            openActivityForResult(ActivityPersonAddAndEdit.class, _Bundle, REQUEST_CODE_EDIT_PERSON);
            
        // 启用人员
        } else if (item.getItemId() == R.id.ContextMenu_PersonList_SetPersonStateEnable) {
            mBusinessPerson.enableModelPerson(mSelectedModelPerson);
            
        // 禁用人员
        } else if (item.getItemId() == R.id.ContextMenu_PersonList_SetPersonStateDisable) {
            showDialog(DIALOG_CONFIRM_PERSON_DISABLE);
            
        // 删除人员
        } else if (item.getItemId() == R.id.ContextMenu_PersonList_DeletePerson) {
            showDialog(DIALOG_CONFIRM_PERSON_DELETE);
        }
        
        mAdapterListViewPersonList.notifyDataSetChanged();
        return super.onContextItemSelected(item);
    }

    @Override
    protected Dialog onCreateDialog(int pDialogId)
    {
        AlertDialog.Builder _AlertDialogBuilder = new AlertDialog.Builder(this);

        // 删除警告对话框
        if (pDialogId == DIALOG_CONFIRM_PERSON_DELETE) {
            _AlertDialogBuilder.setTitle(R.string.title_person_delete)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setMessage(getString(R.string.message_person_check_delete))
                    .setPositiveButton(R.string.ok, this)
                    .setNegativeButton(R.string.cancel, this);
            
        // 禁用警告对话框
        } else if (pDialogId == DIALOG_CONFIRM_PERSON_DISABLE) {
            _AlertDialogBuilder.setTitle(R.string.title_person_disable)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage(getString(R.string.message_person_check_disable))
                    .setPositiveButton(R.string.ok, this)
                    .setNegativeButton(R.string.cancel, this);
        }

        return _AlertDialogBuilder.create();
    }

    /**
     * Dialog按钮的点击事件
     */
    @Override
    public void onClick(DialogInterface dialog, int which)
    {
        // 删除警告对话框
        if (getShowingDialogID() == DIALOG_CONFIRM_PERSON_DELETE) {
            // 确定按钮
            if (which == DialogInterface.BUTTON_POSITIVE) {
                mBusinessPerson.deleteModel(mSelectedModelPerson);
            // 取消按钮
            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                dialog.cancel();
                
            }
            
        // 禁用警告对话框
        } else if (getShowingDialogID() == DIALOG_CONFIRM_PERSON_DISABLE) {
            // 确定按钮
            if (which == DialogInterface.BUTTON_POSITIVE) {
                mBusinessPerson.disableModelPerson(mSelectedModelPerson);
                
            // 取消按钮
            } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                dialog.cancel();
            }
        }
    
        mAdapterListViewPersonList.notifyDataSetChanged();
    }

    /**
     * SlideMenu的点击事件
     */
    @Override
    public void onSlideMenuItemClick(SlideMenu pSlideMenu, SlideMenuItem pSlideMenuItem, AdapterView<?> pParent, View pView, int pPosition, long pId)
    {
        if (pPosition == 0) {
            slideMenuToggle();
            openActivityForResult(ActivityPersonAddAndEdit.class, null, REQUEST_CODE_ADD_PERSON);
        }
    }

    @Override
    protected void initFields()
    {
        mAdapterListViewPersonList = new AdapterListViewPersonList(this);
        mBusinessPerson = new BusinessPerson();
    }

    @Override
    protected void initViews()
    {
        mFramebodyPerson_ListView_PersonList = (ListView) this.findViewById(R.id.FramebodyPerson_ListView_PersonList);
        mFramebodyPerson_ListView_PersonList.setEmptyView(findViewById(R.id.FramebodyPerson_LinearLayout_Empty));
    }

    @Override
    protected void initListeners()
    {
    }

    @Override
    protected void bindData()
    {
        mFramebodyPerson_ListView_PersonList.setAdapter(mAdapterListViewPersonList);
    }

    @Override
    protected void register()
    {
        registerForContextMenu(mFramebodyPerson_ListView_PersonList);
    }

    @Override
    protected Object doInBackground(Object... params)
    {
        initFields();
        return null;
    }

    @Override
    protected void onPostExecute(Object result)
    {
        super.onPostExecute(result);
        initViews();
        initListeners();
        bindData();
        register();
    }

}
